package com.shengyin.ec.controller.comment;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shengyin.ec.model.entity.Comment;
import com.shengyin.ec.model.entity.Goods;
import com.shengyin.ec.model.entity.Personal;
import com.shengyin.ec.service.comment.CommentService;
import com.shengyin.ec.service.goods.GoodsService;

@Controller
public class CommentController {
	@Autowired
	private CommentService commentService;
	@Autowired
	private GoodsService goodsService;

	/**
	 * 发表评论
	 */
	@RequestMapping(value = "/user/comment.do", method = RequestMethod.POST)
	@ResponseBody
	public ModelMap comment(HttpServletRequest request, CommentForm commentForm)
			throws Exception {
		HttpSession session = request.getSession();
		// 获取当前用户
		Personal user = (Personal) session.getAttribute("user");
		// 获取商品
		Goods goods = goodsService.getGoodsById(commentForm.getGoodsId());
		if (user == null || goods == null) {
			return null;
		}
		// 获取ip
		String ip = this.getIp(request);
		// 创建实体对象
		Comment comment = new Comment();
		// 填充属性
		comment.setContent(commentForm.getContent());
		comment.setGoods(goods);
		comment.setScore(commentForm.getScore());
		comment.setUser(user);
		comment.setIp(ip);
		//保存
		commentService.createComment(comment);
		//获取评价列表
		List<CommentForm> commentFormList = findByPage(commentForm.getGoodsId(), 1, 5);
		ModelMap modelMap = new ModelMap();
		modelMap.addAttribute("commentFormList",commentFormList);
		return modelMap;
	}
	/**
	 * 获取评论列表
	 */
	@RequestMapping(value = "user/getCommentList.do", method = RequestMethod.GET)
	@ResponseBody
	public ModelMap getCommentList(HttpServletRequest request, CommentForm commentForm)
			throws Exception {
		//获取评价列表
		List<CommentForm> commentFormList = findByPage(commentForm.getGoodsId(), 1, 5);
		ModelMap modelMap = new ModelMap();
		modelMap.addAttribute("commentFormList",commentFormList);
		return modelMap;
	}
	/**
	 * 取评论列表
	 */
	private List<CommentForm> findByPage(Long goodsId,int offset,int maxResult){
		List<CommentForm> list = new ArrayList<CommentForm>();
		//获取评价列表
		List<Comment> commentList = commentService.findByPage(goodsId, offset, maxResult);
		//装入list
		for(Comment comment : commentList){
			CommentForm cf = new CommentForm();
			cf.setContent(comment.getContent());
			cf.setScore(comment.getScore());
			cf.setUserName(comment.getUser().getUsername());
			list.add(cf);
		}
		return list;
	}
	/**
	 * 获取ip
	 */
	private String getIp(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}
}
